/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.goldsmith;

import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.model.master.Goldsmith;
import com.kmcsoft.spring.SpringUtil;
import com.kmcsoft.utils.i18n;
import java.util.ArrayList;
import java.util.Collection;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author huyvu
 */
public class GoldsmithComposer extends GenericForwardComposer {

    private ShopGenericService service;
    private AnnotateDataBinder binder;
    private AnnotateDataBinder paBinder;
    private Component component;
    private Collection<Goldsmith> lists;
    private Collection<Goldsmith> deleteLists;

    //<editor-fold defaultstate="collapsed" desc="GET/SET">
    public Collection<Goldsmith> getLists() {
        if (lists.isEmpty()) {
            lists.add(new Goldsmith());
        }
        return lists;
    }

    public void setLists(Collection<Goldsmith> lists) {
        this.lists = lists;
    }

    private AnnotateDataBinder getBinder() {
        return binder;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterCompose">
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        if (service == null) {
            service = SpringUtil.getBean("shopGenericService", ShopGenericService.class);
        }
        self.setAttribute("comp", this);
        component = (Component) arg.get("component");
        paBinder = (AnnotateDataBinder) arg.get("binder");
        binder = new AnnotateDataBinder(self);
        lists = service.getGoldsmiths();
        deleteLists = new ArrayList<Goldsmith>();
        self.setAttribute("binder", binder);
        binder.loadAll();

    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onInsertRow">
    public void onInsertRow(ForwardEvent event) {
        Grid grid = ((Grid) event.getOrigin().getTarget().getParent().getParent().getParent());
        lists.add(new Goldsmith());
        getBinder().loadComponent(grid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onRemove">
    public void onRemove(ForwardEvent event) {
        Grid grid = (Grid) event.getOrigin().getTarget().getParent().getParent().getParent();
        if (grid.getModel().getSize() == 1) {
            return;
        }
        Toolbarbutton btton = (Toolbarbutton) event.getOrigin().getTarget();
        Row row = ((Row) btton.getParent());
        Goldsmith selectedRecord = (Goldsmith) row.getValue();
        if (selectedRecord.getId() != 0) {
            if (service.checkUsingGoldsmith(selectedRecord)) {
                throw new WrongValueException(i18n.l("shop.goldsmith.error.delete"));
            }
            deleteLists.add(selectedRecord);
        }
        ((BindingListModelList) grid.getModel()).remove(selectedRecord);
        grid.renderAll();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onCancelModal">
    public void onCancelModal() {
        self.detach();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onClick$save">
    public void onClick$save() {
        checkBeforeSave();
        service.makePersistentAll(lists);
        for (Goldsmith color : deleteLists) {
            service.deletePersistent(color);
        }
        paBinder.loadComponent(component);
        self.detach();

    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkBeforeSave">
    private void checkBeforeSave() {
        for (Goldsmith color : lists) {
            String name = color.getName().trim();
            boolean checkName = false;
            for (Goldsmith clor : lists) {
                if (clor.getName().trim().equalsIgnoreCase(name)) {
                    if (checkName) {
                        throw new WrongValueException(i18n.l("shop.goldsmith.error.duplicateName"));
                    }
                    checkName = true;
                }
            }
        }
    }
    //</editor-fold>
}
